package com.xywl.escort.adapter;

import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.AbsoluteSizeSpan;
import android.widget.Button;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.xywl.escort.R;
import com.xywl.escort.bean.CouponBean;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

public class CouponAdapter extends BaseQuickAdapter<CouponBean, BaseViewHolder> {
    public CouponAdapter(int layoutResId, @Nullable List<CouponBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CouponBean item) {
        ConstraintLayout layout = helper.getView(R.id.layout_coupon);

        TextView textMoney = helper.getView(R.id.text_money);
        if (null != item.amountOfMoneyYuan) {
            int amountOfMoney = item.amountOfMoneyYuan.intValue();
            String money = "￥" + amountOfMoney;

            SpannableString spanString = new SpannableString(money);
            AbsoluteSizeSpan span = new AbsoluteSizeSpan(12, true);
            spanString.setSpan(span, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textMoney.setText(spanString);
        }
        TextView textUseCondition = helper.getView(R.id.text_use_condition);
        if (null != item.condMinPriceYuan) {
            int condMinPrice = item.condMinPriceYuan.intValue();
            String money = "满" + condMinPrice + "元可用";
            textUseCondition.setText(money);
        }
        helper.setText(R.id.text_name, item.name);
        helper.setText(R.id.text_remark, item.remark);
        helper.setText(R.id.text_condition, "使用条件: " + item.condMinDay + "天以上项目");
        if (item.valid.equals("-1")) {
            helper.setText(R.id.text_validity, "有效期至: 永久有效");
        } else {
            String format = getDateFormat(item.validTime);
            helper.setText(R.id.text_validity, "有效期至: ".concat(format));
        }
        Button btnStatus = helper.getView(R.id.btn_status);
        int useStatus = item.useStatus;
        switch (useStatus) {
            case 1:
                btnStatus.setText("立即使用");
                btnStatus.setEnabled(true);
                layout.setAlpha(1.0f);
                break;
            case 2:
                btnStatus.setText("已使用");
                btnStatus.setEnabled(false);
                layout.setAlpha(0.5f);
                break;
            case 3:
                btnStatus.setText("已过期");
                btnStatus.setEnabled(false);
                layout.setAlpha(0.5f);
                break;
            default:
                break;
        }
        helper.addOnClickListener(R.id.btn_status);
    }

    private String getDateFormat(String strData) {
        if (!TextUtils.isEmpty(strData)) {
            final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            final SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());

            Date date;
            try {
                date = simpleDateFormat.parse(strData);
            } catch (ParseException e) {
                e.printStackTrace();
                return strData;
            }

            return simpleDateFormat2.format(date);
        }
        return strData;
    }
}
